<template>
	<view class="contes">
		<view class="top dis dis3 dis2">
			 <!-- <uni-s-input  initValue="1"  id="name" @inputValue="getInputValue"></uni-s-input> -->
			<input type="text" v-model="name" @input="inputChange" class="input" maxlength="6" />
			<image src="../../../static/image/del.png" mode="aspectFit" class="del" @click="delName"></image>
		</view>
		<view class="bootns dis dis2">2-6个字符，仅支持中英文和数字，不支持特殊符号</view>
		<view class="btn contenr" v-if="name" @click="preserve">
			<image src="../../../static/image/Btn.png" mode="aspectFit" class="img"></image>
		</view>
		<view class="btn contenr" v-if="!name">
			<image src="../../../static/img/ico5.png" mode="aspectFit" class="img"></image>
		</view>
	</view>
</template>

<script>
	let app = getApp().globalData;
	export default {
		data() {
			return {
				name: app.user.userName || app.user.nickname,
				is:true
			};
		},
		methods: {
			delName() {
				this.name = '';
				this.inputChange(null)
			},
			inputChange(e){
				let name = this.name ;
				if(!e){
					return this.name = '';
				}
				let that =this;
				let testPs =
					/(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])/;
				if (that.stripscript(e.detail.value,0)  || testPs.test(e.detail.value)) {
					that.$http.isMssage('昵称2-6个字符，仅支持中英文和数字，不支持特殊符号');
					console.log('name',name)
					that.is=false;
					setTimeout(()=>{
						that.is= true
						return that.name = that.stripscript(name,1);
					},1000)
					return
				}else{
					that.name = e.detail.value;
				}
				
			},
			stripscript(str,id) {
				var pattern = new RegExp("[`!@#$^&*()=|{}:;,\\[\\]<>/?！@#￥……&*（）——|{}【】‘；：”“'。，、？]");
				var rs = ""; 
				if(id==1){
					for (var i = 0; i < str.length; i++) { 
						rs = rs+str.substr(i, 1).replace(pattern, ''); 
					} 
					return rs;
				}else{
					return pattern.test(str);
				}
				
			},
			preserve() {
				let that = this;
				if (!that.is) {
					return
				}
				if ( that.name.length < 2 ) {
					that.$http.isMssage('昵称2-6个字符，仅支持中英文和数字，不支持特殊符号');
					console.log('name=', that.name)
					
				} else {

					let pages = getCurrentPages();
					let prevPage = pages[pages.length - 2];
					prevPage.setData({
						name: that.name
					})
					that.$http._post('user/setuserinfo.do', {
						username: that.name,
						avatarurl: app.user.avatarurl,
						sex: app.user.sex,
						birth: app.user.birth,
						phone: app.user.birth,
						city: app.user.city
					}).then(res => {
						console.log(res)
						if (res.data.issuccess == 1) {
							app.user = res.data.data;
							uni.navigateBack({
								delta: 1
							})
						}
					})
				}
			}
		},
		onLoad() {
			console.log(app)
		}
	}
</script>

<style scoped lang="scss">
	@import "../../../common/css/flex.css";
	@import "../../../common/css/index.scss";

	.contes {
		width: 100%;
		height: 98vh;
		// background-color: #fff;
		margin-top: 20upx;

		.top {
			width: 100%;
			height: 120upx;
			// border-bottom: 1px dashed #999;
			background-color: #fff;

			.input {
				width: 70%;
				height: 100%;
				font-size: 32upx;
				font-family: $typeface;
				font-weight: 500;
				color: rgba(51, 51, 51, 1);
				margin-left: 30upx;
			}

			.del {
				width: 44upx;
				height: 44upx;
				margin-right: 30upx;
			}
		}

		.bootns {
			width: 90%;
			height: 80upx;
			margin-left: 32upx;
			font-size: 24upx;
			font-family: $typeface1;
			font-weight: 400;
			color: #999999;
		}

		.btn {
			width: 100%;
			height: 300upx;

			.img {
				width: 500upx;
				height: 88upx;
			}
		}
	}
</style>
